// page/component/new-pages/cart/cart.js
const {gouwu}=require('../../api/index')
Page({
  goToOrderSubmit(){
    wx.navigateTo({
      url: '../Settlement/index',
    })
  },
  data: {
    carts:[],               // 购物车列表
    hasList:false,          // 列表是否有数据
    totalPrice:0,           // 总价，初始为0
    selectAllStatus:false,    // 全选状态，默认全选
    shoplist:[],

    obj:{
        name:"hello"
    }
  },
  onLoad: function (options) {
    // console.log(options)
    let id = options.id
    gouwu({id:id*1}).then((res)=>{
      console.log(res)
      this.setData({
        shoplist:res.data.data
      })
    })
    wx.removeStorageSync('aa');
    wx.setStorageSync('aa', "123")
},
  onShow() {
    this.setData({
      hasList: true,
      carts:[
        {id:1,title:'新鲜芹菜 半斤',image:'/image/s5.png',num:4,price:0.01,selected:true},
        {id:2,title:'素米 500g',image:'/image/s6.png',num:1,price:0.03,selected:true}
      ]
    });
    this.getTotalPrice();
  },
  /**
   * 当前商品选中事件
   */
  selectList(e) {
    const index = e.currentTarget.dataset.index;
    let shoplist = this.data.shoplist;
    const selected = shoplist[index].selected;
    shoplist[index].selected = !selected;
    this.setData({
      shoplist: shoplist
    });
    this.getTotalPrice();
  },

  /**
   * 删除购物车当前商品
   */
  deleteList(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    carts.splice(index,1);
    this.setData({
      carts: carts
    });
    if(!carts.length){
      this.setData({
        hasList: false
      });
    }else{
      this.getTotalPrice();
    }
  },

  /**
   * 购物车全选事件
   */
  selectAll(e) {
    let selectAllStatus = this.data.selectAllStatus;
    selectAllStatus = !selectAllStatus;
    let shoplist = this.data.shoplist;

    for (let i = 0; i < shoplist.length; i++) {
      shoplist[i].selected = selectAllStatus;
    }
    this.setData({
      selectAllStatus: selectAllStatus,
      shoplist: shoplist
    });
    this.getTotalPrice();
  },

  /**
   * 绑定加数量事件
   */
  addCount(e) {
    const index = e.currentTarget.dataset.index;
    let shoplist = this.data.shoplist;
    let quantity = shoplist[index].quantity;
    // if(quantity >= 10){
    //   wx.showToast({
    //     title: '数量超过10',
    //   })
    // }else{
      quantity = quantity += 1;
    // }
    shoplist[index].quantity = quantity;
    this.setData({
      shoplist: shoplist
    });
    this.getTotalPrice();
  },

  /**
   * 绑定减数量事件
   *  
 
   */
  minusCount(e) {
    const index = e.currentTarget.dataset.index;
    let shoplist = this.data.shoplist;
    let quantity = shoplist[index].quantity;
    if(quantity <= 1){
      wx.showToast({
        title: '商品数量少于1',
      })
    }else{
      quantity = quantity - 1;
    }
    
    shoplist[index].quantity = quantity;
    this.setData({
      shoplist: shoplist
    });
    this.getTotalPrice();
  },

  /**
   * 计算总价
   */
  getTotalPrice() {
    let shoplist = this.data.shoplist;                  // 获取购物车列表
    let total = 0;                                      //总价初始值为零
    for(let i = 0; i<shoplist.length; i++) {            // 循环列表得到每个数据
      if(shoplist[i].selected) {                        // 判断选中才会计算价格
        total += shoplist[i].quantity * shoplist[i].price;   // 所有价格加起来
        wx.setStorageSync('aa', total)
      }
    }
     
    this.setData({                                // 最后赋值到data中渲染到页面
      shoplist: shoplist,
      totalPrice: total.toFixed(2)
    });
  }

})